<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <!-- 兼容性 -->
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
    <title>计划编制-职能计划列表</title>
    <script src="/scripts/boot.js"></script>
    <style>
        html,body{
            margin:0;padding: 0;border:0;width:100%;height:100%;overflow:hidden;
        }
        body{
            background: #ffffff!important;
            padding:20px;
        }
    </style>
</head>
<body>
<div id="viewForm">
    <div class="title-list mb-20"><span class="title-listbg"></span><span>职能计划编制</span></div>
    <div>
        <table style="width:100%;">
            <tr>
                <td style="width:100%;">
                    <div class="mb-20 position-rel">
                        <span>计划名称</span><input type="text" id="name" name="name" class="mini-textbox w220" readonly/>
                   		<input  class="mini-hidden w220" id="planId" name="planId"  />
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <div class="mini-fit" style="height:400px;">
        <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" url="/functionPlanNode/listByPage" sizeList="[5,10,20,50]"
         pageSize="20"  idField="id" multiSelect="true" allowCellEdit="true" allowCellSelect="true" editNextOnEnterKey="true"  editNextRowCell="true"
             allowCellValid="true" oncellvalidation="view.onCellValidation"
             allowResize="true" pageSize="20">
            <div property="columns">
                <div field="NO" headerAlign="center" width="40" align="center">编号</div>
                <div field="functionOfficeValue" width="100" align="center" headerAlign="center" allowSort="true">职能中心</div>
                <div field="nodeName" width="120" headerAlign="center" allowSort="true">节点名称</div>
                <div field="nodeProperty" width="100" headerAlign="center" align="center" allowSort="true">节点性质</div>
                <div field="completeStandard" width="120" headerAlign="center" allowSort="true">完成标准</div>
                <div field="beginDate" width="100" headerAlign="center" vtype="required" align="center" allowSort="true" dateFormat="yyyy-MM-dd">计划开始时间
                	 <input property="editor" class="mini-datepicker" style="width:100%;" minWidth="200" />
                </div>
                <div field="endDate" width="100" headerAlign="center" vtype="required" align="center" allowSort="true" dateFormat="yyyy-MM-dd">计划结束时间
                	 <input property="editor" class="mini-datepicker" style="width:100%;" minWidth="200" />
                </div>
                <div field="companyName" width="120" headerAlign="center" allowSort="true">责任单位</div>
                <div field="officeName" width="120" headerAlign="center" align="center" allowSort="true">责任部门</div>
                <div field="postName" width="120" headerAlign="center" align="center" allowSort="true">责任岗位</div>
                <div field="remark" width="120" headerAlign="center" allowSort="true">备注</div>
            </div>
        </div>
    </div>
    <div class="chose-toolbar">
        <a class="chose-button" onclick="CloseWindow('close')">取消</a>
        <a class="chose-button save-button" onclick="view.saveData(0)">保存</a>
        <a class="chose-button save-button" onclick="view.audit()">提交审批</a>
    </div>
</div>
<script type="text/javascript">
    var post_flag,form;
    var view = {
        init: function () {
            post_flag = false; //设置一个对象来控制是否进入AJAX过程
            mini.parse();
            form = new mini.Form("#viewForm");
            mini.get("datagrid1").load();
        },
        onCellValidation:function(e){
            if(e.field == "beginDate"){
                begindate = e.value ;
                if(e.value == null || e.value == ""){
                    e.isValid = false ;
                    e.errorText=("对不起，开始时间不能为空");
                }
            }else if(e.field == "endDate"){
                enddate = e.value ;
                if(e.value == null || e.value == ""){
                    e.isValid = false ;
                    e.errorText=("对不起，结束时间不能为空");
                }
                if(enddate < begindate){
                    e.isValid = false ;
                    e.errorText=("对不起，结束时间不能小于开始时间");
                }
            }
        },
        //审核
        audit:function () {
           var result = view.saveData(1);
           if(!result)return;
   		   var pid = $("#planId").val();
           $.ajax({
				type : "post",
				url : "/loggerService/startPost",
				data : {"id":pid,"formSign":"CDJHGLJHBZJH_01","workFlowId":"CDJHGLJHBZJHSP","jumpUrl":"/planning/drafts/workflow.html?id="+pid,"url":"/act/taskApi/start","exampleEntity":"plan"},
				success : function(msg) {
					if(msg.message==="ok"){
						view.updateAuditState(pid);
					}
				}
			}); 
        }, //改变数据状态
        updateAuditState: function(pid){
        	 $.ajax({
	        	  url:"/keyPlan/audit",
	        	  data:{"planId":pid},
	        	  success:function(data){
	        		  mini.alert(data.message, "提醒");
	        		  if(data.state){
	        			  CloseWindow('close')
	        		  }
	        	  }
        	 });
		},
        //    数据展示
        SetData: function (data) {
            data = mini.clone(data);
            mini.get("datagrid1").load({'plan.id': data.id});
            mini.get("name").setValue(data.name);
            mini.get("planId").setValue(data.id);
        },
        //保存
        saveData:function(b) {
            var grid= mini.get("datagrid1");
            grid.validate();
            if (grid.isValid() == false) {
                mini.alert("请输入计划起止时间");
                var error = grid.getCellErrors()[0];
                grid.beginEditCell(error.record, error.column);
                return false;
            }
            var data = grid.getChanges();
            if(data.length == 0){
            	if(b == 1){
            		return true;
            	}else if(b == 0){
            		return false;
            	}
            }
           	data = mini.encode(data);
       		data = $.parseJSON(data);
       		var ids = [];
       		var beginDates=[];
       		var endDates = [];
       		for(var i=0;i<data.length;i++){
       			ids.push(data[i].id);
       			beginDates.push(data[i].beginDate);
       			endDates.push(data[i].endDate);
       		}
       	    $.ajax({
       	        url: "/functionPlanNode/update",
       	        data: {ids:ids,beginDates:beginDates,endDates:endDates},
       	        type: "post",
       	        success: function (text) {
       	        	grid.reload();
       	        },
       	        error: function (jqXHR, textStatus, errorThrown) {
       	            alert(jqXHR.responseText);
       	        }
       	    });
       	    return true;
           
        }

    };
    view.init();
</script>
</body>
</html>